package com.zhz.mall.portal.controller;

import com.zhz.mall.common.api.CommonResult;
import com.zhz.mall.portal.modules.ums.model.UmsMemberReceiveAddress;
import com.zhz.mall.portal.modules.ums.service.UmsMemberReceiveAddressService;
import io.swagger.annotations.Api;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.*;

import java.util.List;

/***
 * @Author zhouhengzhe
 */
@RestController
@Api("收货地址服务接口")
@RequestMapping("/member/address")
public class MemberAddressController {

    @Autowired
    private UmsMemberReceiveAddressService addressService;

    /**
     * 添加用户地址
     *
     * @param address
     * @return
     */
    @PostMapping("/add")
    public CommonResult<Boolean> add(@RequestBody UmsMemberReceiveAddress address) {
        return addressService.add(address) ? CommonResult.success(true) : CommonResult.failed();
    }

    /**
     * 通过id更新用户地址
     *
     * @param id
     * @param address
     * @return
     */
    @PostMapping("/update/{id}")
    public CommonResult<Boolean> edit(
            @PathVariable Long id,
            @RequestBody UmsMemberReceiveAddress address) {
        address.setId(id);
        return addressService.edit(address) ? CommonResult.success(true) : CommonResult.failed();
    }

    /**
     * 根据id删除收件地址
     *
     * @param id
     * @return
     */
    @PostMapping("/delete/{id}")
    public CommonResult<Boolean> delete(@PathVariable Long id) {
        return addressService.delete(id) ? CommonResult.success(true) : CommonResult.failed();
    }

    /**
     * 获取当前用户地址信息
     *
     * @return
     */
    @GetMapping("/list")
    public CommonResult<List<UmsMemberReceiveAddress>> list() {
        return CommonResult.success(addressService.listByMemberId());
    }

}
